package top.atcra.rpc.transport.utils;

import io.netty.buffer.ByteBuf;

public class HeaderUtil {
    static final byte[] protocol_id = new byte[MessageFormatConstant.PROTOCOL_ID.length];

    public static void checkProtocolVersion(ByteBuf buf) {
        // 校验协议号
        buf.readBytes(protocol_id);
        for (int i = 0; i < protocol_id.length; i++) {
            if (protocol_id[i] != MessageFormatConstant.PROTOCOL_ID[i]) {
                throw new RuntimeException("Protocol id mismatch");
            }
        }
        // 校验版本号
        byte version = buf.readByte();
        if (version > MessageFormatConstant.VERSION) {
            throw new RuntimeException(String.format("Protocol version mismatch: The maximum protocol version supported by the server is %d, but version of data pack is %d", MessageFormatConstant.VERSION, version));
        }
    }
}
